var gulp = require("gulp");
var connect = require('gulp-connect');
var replace = require("gulp-replace");
var stylus = require("gulp-stylus");
var sourcemaps = require("gulp-sourcemaps");
var plumber = require('gulp-plumber'); //差错控制

var path = {
    css: 'EPUB/css/*.css',
    stylus: 'EPUB/css/*.styl',
    o_css: 'EPUB/css',
    xhtml: 'EPUB/xhtml/*.xhtml',
    o_xhtml: 'EPUB/xhtml',
    img: 'EPUB/img'
}
gulp.task('rnpic', function() {
    return gulp.src(path.xhtml)
        .pipe(replace(/\[.+?(\d+).+?\]\.jpg/g, 'pic$1.jpg'))
        .pipe(gulp.dest(path.o_xhtml));
});
gulp.task('stylus', function() {
    return gulp.src(path.stylus)
        .pipe(plumber())
        .pipe(sourcemaps.init())
        .pipe(stylus())
        .pipe(sourcemaps.write('.'))
        .pipe(gulp.dest(path.o_css))
        .pipe(connect.reload());
})
gulp.task('xhtml', function() {
    return gulp.src(path.xhtml)
        .pipe(connect.reload());
})
gulp.task('watch', ['stylus'], function() {
    gulp.watch(path.stylus, ['stylus']);
    gulp.watch(path.xhtml, ['xhtml']);
})
gulp.task('connect', ['watch'], function() {
    connect.server({
        port: 80,
        root: ['EPUB'],
        livereload: true
    });
});
gulp.task('default', ['connect'])
